#include"CS_Animations.h"
#include"CS_Global.h"
#include"CS_Screen.h"
#include<stdlib.h>
#include<string.h>
#include<conio.h>

void stringToTop(unsigned char x, unsigned char startY, const char* string, unsigned long delaySize)
{
	int i=startY;
	unsigned long d=0ul;
	int length = strlen(string);

	while(i >= 0)
	{
		if(i!=startY)
		{
			cclearxy(x, i+1u, length);
		}
		cputsxy(x, i, string);

		for(d=0ul; d<delaySize; ++d);

		--i;
	}
}

void displaySelector(unsigned char x, unsigned char y, bool erase)
{
	unsigned char oldColor = textcolor(COLOR_VIOLET);

	switch(currentSelectorPosition)
	{
	case 0:
		cputcxy(x + 1u, y - 1u, (erase ? CH_SPACE : CH_CHECK));
		break;
	case 1:
		cputcxy(x + 3u, y - 1u, (erase ? CH_SPACE : CH_CHECK));
		break;
	case 2:
		cputcxy(x + 5u, y - 1u, (erase ? CH_SPACE : CH_CHECK));
		break;
	case 3:
		cputcxy(x + 7u, y - 1u, (erase ? CH_SPACE : CH_CHECK));
		break;
	//case 4:
	//	cputcxy(x + 9u, y + 1u, (erase ? CH_SPACE : CH_CHECK));
	//	break;
	//case 5:
	//	cputcxy(x + 9u, y + 3u, (erase ? CH_SPACE : CH_CHECK));
	//	break;
	//case 6:
	//	cputcxy(x + 9u, y + 5u, (erase ? CH_SPACE : CH_CHECK));
	//	break;
	//case 7:
	//	cputcxy(x + 9u, y + 7u, (erase ? CH_SPACE : CH_CHECK));
	//	break;
	}

	(void)textcolor(oldColor);
}

void moveSelectorRight(unsigned char x, unsigned char y)
{
	if(currentSelectorPosition == 3) return;

	displaySelector(x, y, true);
	++currentSelectorPosition;
	displaySelector(x, y, false);
}

void moveSelectorLeft(unsigned char x, unsigned char y)
{
	if(currentSelectorPosition == 0) return;

	displaySelector(x, y, true);
	--currentSelectorPosition;
	displaySelector(x, y, false);
}

void drawDie(unsigned char x, unsigned char y, unsigned char character, bool erase)
{
	if(!erase)
	{
		cputcxy(x, y, CH_ULCORNER);
		cputcxy(x + 1u, y, CH_HLINE);
		cputcxy(x + 2u, y, CH_URCORNER);
		cputcxy(x, y + 1u, CH_VLINE);
		cputcxy(x + 1u, y + 1u, character);
		cputcxy(x + 2u, y + 1u, CH_VLINE);
		cputcxy(x, y + 2u, CH_LLCORNER);
		cputcxy(x + 1u, y + 2u, CH_HLINE);
		cputcxy(x + 2u, y + 2u, CH_LRCORNER);
	}
	else
	{
		cclearxy(x, y, 3);
		cclearxy(x, y + 1u, 3);
		cclearxy(x, y + 2u, 3);
	}
}

void rollDie(unsigned char x, unsigned char y, unsigned char resultIndex, const char* sides)
{
	unsigned char counter;
	int delay;

	drawDie(x + 6, y, CH_SPACE, true);

	for(counter = 0u; counter < 3u; ++counter)
	{
		drawDie(x + counter * 2, y, sides[rand() % DIE_SIZE], false);

		for(delay = 0; delay < 1000; delay += 5 - (counter + 1));

		drawDie(x + counter * 2, y, CH_SPACE, true);
	}

	drawDie(x + counter * 2, y, sides[resultIndex], false);
}

void pointToPlayer(unsigned char x, unsigned char y, unsigned char player, bool display)
{
	(void)textcolor(COLOR_YELLOW);
	cputcxy(x + 11u, y + (player * 4), (display ? '>' : CH_SPACE));
}